// ==================================================================================
// Shared Genomics Project MPI Codebase
// Version 1.0 30/04/2010
//
// (c) 2010 University of Manchester all rights reserved
//
// This file is distributed under the GNU General Public License, Version 2.  
// Please see the file COPYING.txt for more details
// ==================================================================================

/*!
\file 
\brief Implementation of the MPI Command Fillers 
*/

#include <stdio.h>
#include <stdlib.h>

#include "commands.h"
#include "epi_utils.h"

void fill_binary_ped_command(char *command, const char *map, const char *ped, char *id, char *ldir, char *rdir) {
	char *EXE = "binary_ped.exe";
	sprintf(command, "mpiexec -n 1 %s%s --map %s --ped %s --id %s --ldir %s --rdir %s", BIN_PATH, EXE, map, ped, id, ldir, rdir);
}

void fill_pformat_data2_command(char *command, int numprocs, const char *bmap, 
								const char *bped, const char *phe, const char *phenoName,
								int available_procs, const char *run_id, const char *ldir, const char *rdir,
								const char *row_major, int nperm, int mode, double mind, double geno, double maf,
								const char *person_missingness, const char *founders, double hwe)
{
	char *EXE = "pformat_data2.exe";
	sprintf(command, "mpiexec -n %i %s%s --bmap %s --bped %s --phe %s --pheno-name %s --available-procs %i --id %s --ldir %s --rdir %s --row-major %s --nperm %i --mode %i --mind %f  --geno %f --maf %f --missingness %s --founders %s --hwe %f", 
			numprocs, BIN_PATH, EXE, bmap, bped, phe, phenoName, available_procs,
			run_id, ldir, rdir, row_major, nperm, mode,
			mind, geno, maf, person_missingness, founders, hwe);
}

void fill_passoc_bt_ft_command(char *command, int numprocs, const char *run_id, const char *samples,
							   const char *bmap, const char *sped,
							   const char *ldir, const char *rdir,
							   const char *perm, int mode) 
{
	char *EXE = "passoc_bt_ft.exe";

	if (mode == 1) {
		// Permutation calculation across N-cores.
		sprintf(command, "mpiexec -n %i %s%s --id %s --samples %s --bmap %s --sped %s --ldir %s --rdir %s --perm %s", 
				numprocs, BIN_PATH, EXE, run_id, samples, bmap, sped, ldir, rdir, perm);
	} else if (mode == 2) {
		sprintf(command, "mpiexec -n %i %s%s --id %s --ldir %s --rdir %s --merge --sort-by P", 
				numprocs, BIN_PATH, EXE, run_id, ldir, rdir);
	} else if (mode == 3) {
		// Permutation calculation across N-cores.
		sprintf(command, "mpiexec -n %i %s%s --id %s --samples %s --bmap %s --sped %s --ldir %s --rdir %s --perm %s --missed-rank 0 --missed-procs 2", 
				numprocs, BIN_PATH, EXE, run_id, samples, bmap, sped, ldir, rdir, perm);
	}
}

void fill_passoc_qt_ft_command(char *command, int numprocs, const char *run_id, const char *samples,
							   const char *bmap, const char *sped,
							   const char *ldir, const char *rdir,
							   const char *perm, int mode) 
{
	char *EXE = "passoc_qt_ft.exe";

	if (mode == 1) {
		// Permutation calculation across N-cores.
		sprintf(command, "mpiexec -n %i %s%s --id %s --samples %s --bmap %s --sped %s --ldir %s --rdir %s --perm %s", 
				numprocs, BIN_PATH, EXE, run_id, samples, bmap, sped, ldir, rdir, perm);
	} else if (mode == 2) {
		sprintf(command, "mpiexec -n %i %s%s --id %s --ldir %s --rdir %s --merge --sort-by P", 
				numprocs, BIN_PATH, EXE, run_id, ldir, rdir);
	} else if (mode == 3) {
		// Permutation calculation across N-cores.
		sprintf(command, "mpiexec -n %i %s%s --id %s --samples %s --bmap %s --sped %s --ldir %s --rdir %s --perm %s --missed-rank 0 --missed-procs 2", 
				numprocs, BIN_PATH, EXE, run_id, samples, bmap, sped, ldir, rdir, perm);
	}
}

void fill_pmodel_ft_command(char *command, int numprocs, const char *run_id, const char *samples,
							   const char *bmap, const char *sped,
							   const char *ldir, const char *rdir,
							   const char *perm, int mode) 
{
	char *EXE = "pmodel_ft.exe";

	if (mode == 1) {
		// Permutation calculation across N-cores.
		sprintf(command, "mpiexec -n %i %s%s --id %s --samples %s --bmap %s --sped %s --ldir %s --rdir %s --perm %s --genotypic", 
				numprocs, BIN_PATH, EXE, run_id, samples, bmap, sped, ldir, rdir, perm);
	} else if (mode == 2) {
		sprintf(command, "mpiexec -n %i %s%s --id %s --ldir %s --rdir %s --merge --sort-by P --genotypic", 
				numprocs, BIN_PATH, EXE, run_id, ldir, rdir);
	} else if (mode == 3) {
		// Permutation calculation across N-cores.
		sprintf(command, "mpiexec -n %i %s%s --id %s --samples %s --bmap %s --sped %s --ldir %s --rdir %s --perm %s --missed-rank 0 --missed-procs 2 --genotypic", 
				numprocs, BIN_PATH, EXE, run_id, samples, bmap, sped, ldir, rdir, perm);
	}
}

void fill_fast_pepi_bt_v2_command(char *command, int numprocs, const char *run_id, const char *samples,
							   const char *bmap, const char *sped,
							   const char *ldir, const char *rdir,
							   int nperm, int mode) 
{
	char *EXE = "fast_pepi_bt_v2.exe";

	if (mode == MODE_NON_PERMUTED) {
		// Permutation calculation across N-cores.
		sprintf(command, "mpiexec -n %i %s%s --id %s --samples %s --bmap %s --sped %s --ldir %s --rdir %s --mode %i", 
				numprocs, BIN_PATH, EXE, run_id, samples, bmap, sped, ldir, rdir, mode);
	} else if (mode == MODE_NON_PERMUTED_MERGE) {
		// Permutation calculation across N-cores.
		sprintf(command, "mpiexec -n %i %s%s --id %s --samples %s --bmap %s --sped %s --ldir %s --rdir %s --mode %i --epi_alpha1 0.10", 
				numprocs, BIN_PATH, EXE, run_id, samples, bmap, sped, ldir, rdir, mode);
	} else if (mode == MODE_STOP) {
		// Permutation calculation across N-cores.
		sprintf(command, "mpiexec -n %i %s%s --id %s --samples %s --bmap %s --sped %s --ldir %s --rdir %s --mode %i --sort-by P", 
				numprocs, BIN_PATH, EXE, run_id, samples, bmap, sped, ldir, rdir, mode);
	} else if (mode == MODE_PERMUTED) {
		// Permutation calculation across N-cores.
		sprintf(command, "mpiexec -n %i %s%s --id %s --samples %s --bmap %s --sped %s --ldir %s --rdir %s --mode %i --nperm %i", 
				numprocs, BIN_PATH, EXE, run_id, samples, bmap, sped, ldir, rdir, mode, nperm);
	} else if (mode == MODE_PERMUTED_MERGE) {
		// Permutation calculation across N-cores.
		sprintf(command, "mpiexec -n %i %s%s --id %s --samples %s --bmap %s --sped %s --ldir %s --rdir %s --mode %i --nperm %i", 
				numprocs, BIN_PATH, EXE, run_id, samples, bmap, sped, ldir, rdir, mode, nperm);
	} 
}

void fill_solcol_command(char *command, int numprocs, const char *run_id, 
			const char *covar, const char *covarColName,
			const char *ldir, const char *rdir,
			int nperm) 
{
	char *EXE = "solcol.exe";
	sprintf(command, "mpiexec -n %i %s%s --id %s --covar %s --ldir %s --rdir %s --covar-name %s --nperm %i --include-missing", 
			numprocs, BIN_PATH, EXE, run_id, covar, ldir, rdir, covarColName, nperm);
}

void fill_pcmh_ft_command(char *command, int numprocs, const char *run_id, const char *samples,
							   const char *bmap, const char *sped,
							   const char *ldir, const char *rdir,
							   const char *perm, int mode, const char *sol, double ci) 
{
	char *EXE = "pcmh_bt.exe";

	if (mode == 1) {
		// Permutation calculation across N-cores.
		sprintf(command, "mpiexec -n %i %s%s --id %s --samples %s --bmap %s --sped %s --ldir %s --rdir %s --perm %s --sol %s --ci %f", 
				numprocs, BIN_PATH, EXE, run_id, samples, bmap, sped, ldir, rdir, perm, sol, ci);
	} else if (mode == 2) {
		sprintf(command, "mpiexec -n %i %s%s --id %s --samples %s --bmap %s --sped %s --ldir %s --rdir %s --perm %s --sol %s --ci %f --merge --sort-by P", 
				numprocs, BIN_PATH, EXE, run_id, samples, bmap, sped, ldir, rdir, perm, sol, ci);
	} 
}

void fill_pepi_bt_v2_command(char *command, int numprocs, const char *run_id, const char *samples,
							   const char *bmap, const char *iped,
							   const char *ldir, const char *rdir,
							   int nperm, int mode) 
{
	char *EXE = "pepi_bt_v2.exe";

	if (mode == MODE_NON_PERMUTED) {
		// Permutation calculation across N-cores.
		sprintf(command, "mpiexec -n %i %s%s --id %s --samples %s --bmap %s --iped %s --ldir %s --rdir %s --mode %i", 
				numprocs, BIN_PATH, EXE, run_id, samples, bmap, iped, ldir, rdir, mode);
	} else if (mode == MODE_NON_PERMUTED_MERGE) {
		// Permutation calculation across N-cores.
		sprintf(command, "mpiexec -n %i %s%s --id %s --samples %s --bmap %s --iped %s --ldir %s --rdir %s --mode %i --epi_alpha1 0.10", 
				numprocs, BIN_PATH, EXE, run_id, samples, bmap, iped, ldir, rdir, mode);
	} else if (mode == MODE_STOP) {
		// Permutation calculation across N-cores.
		sprintf(command, "mpiexec -n %i %s%s --id %s --samples %s --bmap %s --iped %s --ldir %s --rdir %s --mode %i --sort-by P", 
				numprocs, BIN_PATH, EXE, run_id, samples, bmap, iped, ldir, rdir, mode);
	} else if (mode == MODE_PERMUTED) {
		// Permutation calculation across N-cores.
		sprintf(command, "mpiexec -n %i %s%s --id %s --samples %s --bmap %s --iped %s --ldir %s --rdir %s --mode %i --nperm %i", 
				numprocs, BIN_PATH, EXE, run_id, samples, bmap, iped, ldir, rdir, mode, nperm);
	} else if (mode == MODE_PERMUTED_MERGE) {
		// Permutation calculation across N-cores.
		sprintf(command, "mpiexec -n %i %s%s --id %s --samples %s --bmap %s --iped %s --ldir %s --rdir %s --mode %i --nperm %i", 
				numprocs, BIN_PATH, EXE, run_id, samples, bmap, iped, ldir, rdir, mode, nperm);
	} 
}



void fill_pepi_qt_v2_command(char *command, int numprocs, const char *run_id, const char *samples,
							   const char *bmap, const char *iped,
							   const char *ldir, const char *rdir,
							   int nperm, int mode) 
{
	char *EXE = "pepi_qt_v2.exe";

	if (mode == MODE_NON_PERMUTED) {
		// Permutation calculation across N-cores.
		sprintf(command, "mpiexec -n %i %s%s --id %s --samples %s --bmap %s --iped %s --ldir %s --rdir %s --mode %i", 
				numprocs, BIN_PATH, EXE, run_id, samples, bmap, iped, ldir, rdir, mode);
	} else if (mode == MODE_NON_PERMUTED_MERGE) {
		// Permutation calculation across N-cores.
		sprintf(command, "mpiexec -n %i %s%s --id %s --samples %s --bmap %s --iped %s --ldir %s --rdir %s --mode %i --epi_alpha1 0.10", 
				numprocs, BIN_PATH, EXE, run_id, samples, bmap, iped, ldir, rdir, mode);
	} else if (mode == MODE_STOP) {
		// Permutation calculation across N-cores.
		sprintf(command, "mpiexec -n %i %s%s --id %s --samples %s --bmap %s --iped %s --ldir %s --rdir %s --mode %i --sort-by P", 
				numprocs, BIN_PATH, EXE, run_id, samples, bmap, iped, ldir, rdir, mode);
	} else if (mode == MODE_PERMUTED) {
		// Permutation calculation across N-cores.
		sprintf(command, "mpiexec -n %i %s%s --id %s --samples %s --bmap %s --iped %s --ldir %s --rdir %s --mode %i --nperm %i", 
				numprocs, BIN_PATH, EXE, run_id, samples, bmap, iped, ldir, rdir, mode, nperm);
	} else if (mode == MODE_PERMUTED_MERGE) {
		// Permutation calculation across N-cores.
		sprintf(command, "mpiexec -n %i %s%s --id %s --samples %s --bmap %s --iped %s --ldir %s --rdir %s --mode %i --nperm %i", 
				numprocs, BIN_PATH, EXE, run_id, samples, bmap, iped, ldir, rdir, mode, nperm);
	} 
}
